package Noyau;

import java.util.Random;

public class Guichet extends CheminBagage
{
    private LoiGuichet loi;
    private double parametre;
    private long cycleCount = 0;
   
    public Guichet()
    {
    	
    }
    
    public Guichet(LoiGuichet newLoi, float newParametre) 
    {
		super();
		loi = newLoi;
		parametre = newParametre;
	}

	public LoiGuichet getLoi()  
	{
        return this.loi;
    }
	
	public void setLoi(LoiGuichet value)
	{
		this.loi = value;
	}

    public double getParametre()  
    {
        return this.parametre;
    }
    
    public void setParametre(double value)
    {
    	this.parametre = value;
    }
    
    public boolean CreerBagage(Toboggan arrivee)
    {
    	if (this.bagages.size() > 0)
    	{
    		return false;
    	}
    	
    	Bagage bagage = new Bagage(this, arrivee); 
    	this.CreerBagage(bagage);
    	
    	return true;
    }
    
    @Override
    public boolean PeutAjouterBagage(Bagage bagage)
    {
    	return false;
    }

	@Override
	//useless?
	public boolean AjouterBagage(Bagage bagage) 
	{
		return false;
	}

	@Override
	public void TickHorloge() 
	{
		//switch depending on the Loi
		//	case 1 : pseudo random
		//	case 2: Gauss
		//	case 3: constant
		//calculate if a Bagage must be created	
		
		if (!this.aeroport.isModeManuel())
		{
			boolean createNewBagage = false;
			
			switch (this.loi)
			{
				case Constant:
					if (this.cycleCount % this.parametre == 0)
					{
						createNewBagage = true;
					}
					
					break;
				case Random:
					Random rdm = new Random();
					if (this.cycleCount % (rdm.nextInt((int)this.parametre) + 1) == 0)
					{
						createNewBagage = true;
					}
					
					break;
				case Gauss:
					// TODO.
					
					break;
			}
			
			if (createNewBagage)
			{
		    	Toboggan arrivee = this.aeroport.getRandomToboggan();
		    	if (arrivee != null)
		    	{			
		    		this.CreerBagage(arrivee);
		    	}
			}
		}
		
		this.cycleCount++;
		
		if (this.sortie == null)
		{
			return;
		}
		
		// If we have a bagage, we try to give it to the next element.
		if (this.bagages.size() > 0)
		{
			Bagage bagage = this.bagages.get(0);
			
			if (!this.sortie.AjouterBagage(bagage))
			{
				return;
			}
			
			this.bagages.remove(bagage);			

	    	this.setChanged();
	    	this.notifyObservers();
		}
	}
	
	@Override
	public int getZBuffer()
	{
		return 10;
	}
}
